﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using CRM.Entity;

using CRM.BLL;
using DP.Web.UI.Controls;
using DP.Web.UI;
using DP.Common;


namespace CRM.Web.Pop
{
    public partial class CustomerInfo : Common.PageBase
    {
        private UserLoginInfo _userLoginInfo;
        public string ModifyUrl = "Modify.aspx?CUSTOMERID={0}";
        //V_CUSTOMERVEHICLE_BLL bll = new V_CUSTOMERVEHICLE_BLL();
        T_CUSTOMER_BLL bll = new T_CUSTOMER_BLL();
        T_DICTIONARY_BLL dBll = new T_DICTIONARY_BLL();

        /// <summary>
        /// 配置
        /// </summary>
        protected T_CONFIG_BLL configBll = new T_CONFIG_BLL();


        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            UserLoginInfo.CheckLoginInfo();
            _userLoginInfo = UserLoginInfo.GetLoginInfo();
            gvList.PageSize = StringHelper.ToInt(configBll.GetCacheValueByName(this.Page, "PageSize"), 20);
            PageHelper.SetPrePageUrl(this.Page);

        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                GetUrlQueryToControl();
                gvDataBinding();
            }
        }

        #region 页面初始化数据绑定




        #endregion

        /// <summary>
        /// 获取 URL 上的参数存入当页面 控件中。
        /// </summary>
        private void GetUrlQueryToControl()
        {
            PageHelper.GetUrlQueryToControl(this.Page);
        }

        /// <summary>
        /// 生成查询条件字符串
        /// </summary>
        private StringBuilder GenerationSQLString()
        {
            StringBuilder sql = new StringBuilder();
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERID"]))
            {
                sql.Append(" and CUSTOMERID like '%" + Request.QueryString["CUSTOMERID"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERNAME"]))
            {
                sql.Append(" and CUSTOMERNAME like '%" + Request.QueryString["CUSTOMERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FIRSTCONTACTTEL"]))
            {
                sql.Append(" and FIRSTCONTACTTEL like '%" + Request.QueryString["FIRSTCONTACTTEL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["MOBILEPHONE"]))
            {
                sql.Append(" and (MOBILEPHONE = '" + Request.QueryString["MOBILEPHONE"] + "' or HOMEPHONE = '" + Request.QueryString["MOBILEPHONE"] + "' or  FIRSTCONTACTTEL = '" + Request.QueryString["MOBILEPHONE"] + "' ) ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["HOMEPHONE"]))
            {
                sql.Append(" and HOMEPHONE like '%" + Request.QueryString["HOMEPHONE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ADDRESS"]))
            {
                sql.Append(" and ADDRESS like '%" + Request.QueryString["ADDRESS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["COUNTRY"]))
            {
                sql.Append(" and COUNTRY like '%" + Request.QueryString["COUNTRY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PROVINCE"]))
            {
                sql.Append(" and PROVINCE like '%" + Request.QueryString["PROVINCE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["PREFECTURECITY"]))
            {
                sql.Append(" and PREFECTURECITY like '%" + Request.QueryString["PREFECTURECITY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CITY"]))
            {
                sql.Append(" and CITY like '%" + Request.QueryString["CITY"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VILLAGETOWN"]))
            {
                sql.Append(" and VILLAGETOWN like '%" + Request.QueryString["VILLAGETOWN"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VILLAGE"]))
            {
                sql.Append(" and VILLAGE like '%" + Request.QueryString["VILLAGE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["GROUPTEAM"]))
            {
                sql.Append(" and GROUPTEAM like '%" + Request.QueryString["GROUPTEAM"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERBASE"]))
            {
                sql.Append(" and CUSTOMERBASE like '%" + Request.QueryString["CUSTOMERBASE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERTYPE"]))
            {
                sql.Append(" and CUSTOMERTYPE like '%" + Request.QueryString["CUSTOMERTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CUSTOMERLEVEL"]))
            {
                sql.Append(" and CUSTOMERLEVEL like '%" + Request.QueryString["CUSTOMERLEVEL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["SPECIALTYPE"]))
            {
                sql.Append(" and SPECIALTYPE like '%" + Request.QueryString["SPECIALTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BIGCUSTOMERTYPE"]))
            {
                sql.Append(" and BIGCUSTOMERTYPE like '%" + Request.QueryString["BIGCUSTOMERTYPE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATUS"]))
            {
                sql.Append(" and STATUS like '%" + Request.QueryString["STATUS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["IDNUMBER"]))
            {
                sql.Append(" and IDNUMBER like '%" + Request.QueryString["IDNUMBER"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["EMAIL"]))
            {
                sql.Append(" and EMAIL like '%" + Request.QueryString["EMAIL"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ZIPCODE"]))
            {
                sql.Append(" and ZIPCODE like '%" + Request.QueryString["ZIPCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FAX"]))
            {
                sql.Append(" and FAX like '%" + Request.QueryString["FAX"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("CREATETIME", Request.QueryString["CREATETIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATETIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("CREATETIME", Request.QueryString["CREATETIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLEID"]))
            {
                sql.Append(" and CREATEPEOPLEID = '" + Request.QueryString["CREATEPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["CREATEPEOPLE"]))
            {
                sql.Append(" and CREATEPEOPLE like '%" + Request.QueryString["CREATEPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLEID"]))
            {
                sql.Append(" and LASTMODIFYPEOPLEID = '" + Request.QueryString["LASTMODIFYPEOPLEID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYPEOPLE"]))
            {
                sql.Append(" and LASTMODIFYPEOPLE like '%" + Request.QueryString["LASTMODIFYPEOPLE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["LASTMODIFYTIMEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("LASTMODIFYTIME", Request.QueryString["LASTMODIFYTIMEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["REMARK"]))
            {
                sql.Append(" and REMARK like '%" + Request.QueryString["REMARK"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["STATUSLOG"]))
            {
                sql.Append(" and STATUSLOG like '%" + Request.QueryString["STATUSLOG"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FTIMESTAMPBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("FTIMESTAMP", Request.QueryString["FTIMESTAMPBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["FTIMESTAMPEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("FTIMESTAMP", Request.QueryString["FTIMESTAMPEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["AGE"]))
            {
                sql.Append(" and AGE like '%" + Request.QueryString["AGE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ISACCEPTSMS"]))
            {
                sql.Append(" and ISACCEPTSMS like '%" + Request.QueryString["ISACCEPTSMS"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ISACCEPTINFORMATION"]))
            {
                sql.Append(" and ISACCEPTINFORMATION like '%" + Request.QueryString["ISACCEPTINFORMATION"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BIRTHDATEBegin"]))
            {
                sql.Append(" and " + bll._dal.GetBeginDateString("BIRTHDATE", Request.QueryString["BIRTHDATEBegin"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["BIRTHDATEEnd"]))
            {
                sql.Append(" and " + bll._dal.GetEndDateString("BIRTHDATE", Request.QueryString["BIRTHDATEEnd"].Trim()) + " ");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERID"]))
            {
                sql.Append(" and DEALERID = '" + Request.QueryString["DEALERID"] + "'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERNAME"]))
            {
                sql.Append(" and DEALERNAME like '%" + Request.QueryString["DEALERNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["DEALERCODE"]))
            {
                sql.Append(" and DEALERCODE like '%" + Request.QueryString["DEALERCODE"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ORGNAME"]))
            {
                sql.Append(" and ORGNAME like '%" + Request.QueryString["ORGNAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["ZONENAME"]))
            {
                sql.Append(" and ZONENAME like '%" + Request.QueryString["ZONENAME"] + "%'");
            }
            if (!String.IsNullOrEmpty(Request.QueryString["VINCODE"]) || !String.IsNullOrEmpty(Request.QueryString["ENGINENUMBER"]))
            {
                sql.Append(" and CUSTOMERID in ( select CUSTOMERID from t_vehiclecustomer where 1=1 ");
                if (!String.IsNullOrEmpty(Request.QueryString["VINCODE"]))
                {
                    sql.Append(" and VINCODE like '%" + Request.QueryString["VINCODE"] + "%'");
                }
                if (!String.IsNullOrEmpty(Request.QueryString["ENGINENUMBER"]))
                {
                    sql.Append(" and ENGINENUMBER like '%" + Request.QueryString["ENGINENUMBER"] + "%'");
                }
                sql.Append(" ) ");
            }
            if (sql.Length == 0)
            {
                return sql;
            }
            if (!UserLoginInfo.IsAdministrator())
            {
                sql.Append(" and BUSINESSDIVISIONCODE = '" + _userLoginInfo.UserEntity.BUSINESSDIVISIONCODE.Trim() + "'");
                UserType userType = UserLoginInfo.GetCurrentUserType();
                switch (userType)
                {
                    case UserType.中心部门:
                    case UserType.供应商:
                    case UserType.市场部:
                        {

                        }
                        break;
                    case UserType.服务商:
                        {
                            sql.Append(String.Format(" and ( not exists (select 1 from  t_salevehicle sv  where sv.customercode = t_customer.customercode) or customercode in (select customercode from t_salevehicle sv where sv.stationcode = '{0}' ))   ", UserLoginInfo.GetCurrentDepartmentCode()));
                        }
                        break;
                    case UserType.经销商:
                        {
                            sql.Append(String.Format("  and ( not exists (select 1 from  t_salevehicle sv  where sv.customercode = t_customer.customercode) or customercode in (select customercode from t_salevehicle sv where sv.dealercode = '{0}' )   ", UserLoginInfo.GetCurrentDepartmentCode()));
                        }
                        break;
                }
            }

            return sql;

        }

        /// <summary>
        /// 页面数据绑定
        /// </summary>
        private void gvDataBinding()
        {
            #region 查询条件字符串
            StringBuilder sql = GenerationSQLString();
            #endregion

            List<T_CUSTOMER_Entity> list;

            if (sql.Length == 0)
            {
                gvList.DataSource = new List<T_CUSTOMER_Entity>();
                gvList.DataKeyNames = new string[] { "CUSTOMERID" };
                gvList.DataBind();
                return;
            }
            //获取总的记录条数
            gvList.RecordCount = (int)bll.GetCount(sql.ToString());

            list = bll.GetList(sql.ToString(), gvList.CurrentPageIndex * gvList.PageSize, gvList.PageSize, gvList.SortConditions);

            gvList.DataSource = list;
            gvList.DataKeyNames = new string[] { "CUSTOMERID" };
            gvList.DataBind();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            PageHelper.GetControlToUrlQuery(this.Page, ref url);
            Response.Redirect(url);
        }


        protected void btnAdd_Click(object sender, EventArgs e)
        {


        }


        /// <summary>
        /// Selecteds the command.
        /// </summary>
        /// <param name="customerId">The customer id.</param>
        /// <param name="customerCode">The customer code.</param>
        /// <returns></returns>
        public string SelectedCommand(string customerId, string customerCode)
        {
            string fun = "return setValue('{0}','{1}'); ";
            fun = String.Format(fun, customerId, customerCode);
            return fun;
        }

        #region GridView 事件
        /// <summary>
        /// GridView 删除事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
        }

        /// <summary>
        /// GridView 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string temp = string.Empty;
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {
                        GridView gv = (sender as GridView);
                        for (int i = 0; i < gv.Columns.Count; i++)
                        {
                            TemplateField tf = gv.Columns[i] as TemplateField;
                            if (tf != null)
                            {
                                #region 存储值 转换成  显示值

                                switch (tf.HeaderText)
                                {
                                    case "操作":
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;
                                    default:
                                        {
                                            e.Row.Cells[i].Attributes.Add("style", "white-space: nowrap;");
                                        }
                                        break;

                                }

                                #endregion
                            }

                            BoundField bf = gv.Columns[i] as BoundField;
                            if (bf != null)
                            {
                                #region 存储值 转换成  显示值

                                switch (bf.DataField)
                                {
                                    case "CUSTOMERBASE":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = dBll.GetCacheValueByTypeKey(this.Page, "CustomerBase", temp);
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception ex)
                                            {
                                            }
                                        }
                                        break;
                                    case "CUSTOMERTYPE":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = dBll.GetCacheValueByTypeKey(this.Page, "CustomerType", temp);
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception ex)
                                            {
                                            }
                                        }
                                        break;
                                    case "CUSTOMERLEVEL":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = dBll.GetCacheValueByTypeKey(this.Page, "CustomerLevel", temp);
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception ex)
                                            {
                                            }
                                        }
                                        break;
                                    case "STATUS":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = dBll.GetCacheValueByTypeKey(this.Page, "CustomerStatus", temp);
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception ex)
                                            {
                                            }
                                        }
                                        break;
                                    case "BIGCUSTOMERTYPE":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = dBll.GetCacheValueByTypeKey(this.Page, "BigCustomerType", temp);
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception ex)
                                            {
                                            }
                                        }
                                        break;
                                    case "SPECIALTYPE":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp =
                                                    ((CustomerSpecialType)
                                                     Enum.Parse(typeof (CustomerSpecialType), temp)).ToString();
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception ex)
                                            {
                                            }
                                        }
                                        break;
                                    case "ISACCEPTSMS":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp = ((IsAcceptSMS) Enum.Parse(typeof (IsAcceptSMS), temp)).ToString();
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch (Exception ex)
                                            {
                                            }
                                        }
                                        break;
                                    case "ISACCEPTINFORMATION":
                                        {
                                            try
                                            {
                                                temp = e.Row.Cells[i].Text.Trim();
                                                temp =
                                                    ((IsAcceptInformation)
                                                     Enum.Parse(typeof (IsAcceptInformation), temp)).ToString();
                                                e.Row.Cells[i].Text = temp;
                                            }
                                            catch(Exception ex)
                                            {
                                            }
                                        }
                                        break;
                                    default:
                                        {

                                        }
                                        break;
                                }

                                #endregion
                            }
                        }
                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }

        /// <summary>
        /// GridView 行创建事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvList_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.DataRow:
                    {

                    }
                    break;
                case DataControlRowType.Header:
                    {

                    }
                    break;
                case DataControlRowType.Pager:
                    {

                    }
                    break;
                case DataControlRowType.EmptyDataRow:
                    {


                    }
                    break;
                default:

                    break;
            }

        }
        #endregion

    }
}
